package cn.iocoder.yudao.module.signature.controller.admin;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.module.signature.validatap12status.dto.BaseResponse;
import cn.iocoder.yudao.module.signature.validatap12status.dto.CertAndMobileprovisionDTO;
import cn.iocoder.yudao.module.signature.validatap12status.dto.CertStatusDTO;
import cn.iocoder.yudao.module.signature.validatap12status.dto.CertInfoDTO;
import cn.iocoder.yudao.module.signature.validatap12status.util.CertUtil;
import cn.iocoder.yudao.module.signature.validatap12status.util.OCSPUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author liujq
 * @date 20241026 22:02
 */
@Tag(name = "管理后台 - 证书")
@RestController
@RequestMapping("/signature/certification")
@Validated
public class CertificationController {

    private static final Integer RETRY_COUNT = 3;

    @PostMapping("/checkP12")
    @Operation(summary = "验证P12证书状态")
    public BaseResponse<CertStatusDTO> check(MultipartFile cert, String password) throws Exception {
        Assert.notNull(cert, "file can not be null");
        String p12Str = Base64Encoder.encode(cert.getBytes());
        CertStatusDTO certStatusDTO = OCSPUtil.checkCertStatus(p12Str, password, RETRY_COUNT);
        CertUtil.getCertInfo(p12Str, password, certStatusDTO);
        BaseResponse<CertStatusDTO> certStatusDTOBaseReponse = new BaseResponse();
        certStatusDTOBaseReponse.setCode(200);
        certStatusDTOBaseReponse.setMsg("success");
        certStatusDTOBaseReponse.setData(certStatusDTO);
        return certStatusDTOBaseReponse;
    }

    @PostMapping("/checkMobileProvesion")
    @Operation(summary = "验证证书和描述文件")
    public BaseResponse<CertAndMobileprovisionDTO> checkMobileProvesion(MultipartFile cert, MultipartFile mobileprovision, String password) throws Exception {
        Assert.notNull(cert, "cert can not be null");
        Assert.notNull(mobileprovision, "mobileprovision can not be null");
        Assert.notBlank(password, "password can not blank");
        
        String p12Str = Base64Encoder.encode(cert.getBytes());
        String mobileContent = new String(mobileprovision.getBytes(), "UTF-8");
        
        CertAndMobileprovisionDTO certAndMobileprovisionDTO = CertUtil.checkMobileProvesion(p12Str, mobileContent, password);
        
        BaseResponse<CertAndMobileprovisionDTO> response = new BaseResponse<>();
        response.setCode(200);
        response.setMsg("success");
        response.setData(certAndMobileprovisionDTO);
        return response;
    }

    @PostMapping("/changePassword")
    @Operation(summary = "修改P12证书密码")
    public BaseResponse<String> changePassword(MultipartFile cert, String currentPassword) throws Exception {
        Assert.notNull(cert, "cert can not be null");
        Assert.notBlank(currentPassword, "currentPassword can not blank");
        String p12Str = Base64Encoder.encode(cert.getBytes());
        String newP12Str = OCSPUtil.changeP12Password(p12Str, currentPassword);
        BaseResponse<String> response = new BaseResponse();
        response.setCode(200);
        response.setMsg("success");
        response.setData(newP12Str);
        return response;
    }

    @PostMapping("/getCertInfo")
    @Operation(summary = "获取证书详细信息")
    public BaseResponse<CertInfoDTO> getCertInfo(MultipartFile cert, String password) throws Exception {
        Assert.notNull(cert, "cert can not be null");
        Assert.notBlank(password, "password can not blank");
        String p12Str = Base64Encoder.encode(cert.getBytes());
        CertInfoDTO certInfo = OCSPUtil.getCertAllInfo(p12Str, password);
        BaseResponse<CertInfoDTO> response = new BaseResponse();
        response.setCode(200);
        response.setMsg("success");
        response.setData(certInfo);
        return response;
    }


}
